<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>使用 GCD 并发编程 | KnightSama‘s Blog</title>
    <meta name="description" content="Hello, my friend!">
    <link rel="icon" href="/favicon.ico">
  <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
  <script src="https://cdn.jsdelivr.net/npm/react/umd/react.production.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/react-dom/umd/react-dom.production.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/@babel/standalone/babel.min.js"></script>
    
    <link rel="preload" href="/assets/css/0.styles.c4ea52a5.css" as="style"><link rel="preload" href="/assets/js/app.a2483de7.js" as="script"><link rel="preload" href="/assets/js/4.0078dadf.js" as="script"><link rel="preload" href="/assets/js/1.c3e7d2b0.js" as="script"><link rel="preload" href="/assets/js/15.92adafd4.js" as="script"><link rel="prefetch" href="/assets/js/10.265e4f40.js"><link rel="prefetch" href="/assets/js/11.a3a2f8c1.js"><link rel="prefetch" href="/assets/js/12.1910f8cc.js"><link rel="prefetch" href="/assets/js/13.229fa478.js"><link rel="prefetch" href="/assets/js/14.c27d4a17.js"><link rel="prefetch" href="/assets/js/16.bcee7c14.js"><link rel="prefetch" href="/assets/js/17.ee1bcf41.js"><link rel="prefetch" href="/assets/js/18.fa3c3a4e.js"><link rel="prefetch" href="/assets/js/19.1a943c57.js"><link rel="prefetch" href="/assets/js/20.44781dae.js"><link rel="prefetch" href="/assets/js/21.1fe46223.js"><link rel="prefetch" href="/assets/js/22.6f7d46c2.js"><link rel="prefetch" href="/assets/js/23.56223d02.js"><link rel="prefetch" href="/assets/js/24.e7cad43b.js"><link rel="prefetch" href="/assets/js/25.b2902552.js"><link rel="prefetch" href="/assets/js/26.70698c07.js"><link rel="prefetch" href="/assets/js/27.6ee3d263.js"><link rel="prefetch" href="/assets/js/28.ce131de6.js"><link rel="prefetch" href="/assets/js/29.90552487.js"><link rel="prefetch" href="/assets/js/30.b3291ccc.js"><link rel="prefetch" href="/assets/js/31.ea8c441e.js"><link rel="prefetch" href="/assets/js/32.3e29bbeb.js"><link rel="prefetch" href="/assets/js/33.65e78c5e.js"><link rel="prefetch" href="/assets/js/34.3ba19d7c.js"><link rel="prefetch" href="/assets/js/35.66d3fc4a.js"><link rel="prefetch" href="/assets/js/36.f5725281.js"><link rel="prefetch" href="/assets/js/37.f5b0b5ff.js"><link rel="prefetch" href="/assets/js/38.3cae3054.js"><link rel="prefetch" href="/assets/js/39.47dc1ffe.js"><link rel="prefetch" href="/assets/js/5.f0714e2e.js"><link rel="prefetch" href="/assets/js/6.53240a5f.js"><link rel="prefetch" href="/assets/js/7.dc126a6b.js"><link rel="prefetch" href="/assets/js/8.86452400.js"><link rel="prefetch" href="/assets/js/9.16ef06de.js"><link rel="prefetch" href="/assets/js/vendors~flowchart.734d5d92.js">
    <link rel="stylesheet" href="/assets/css/0.styles.c4ea52a5.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div><div class="theme-container" data-v-0cb1c278><div data-v-0cb1c278><div id="loader-wrapper" class="loading-wrapper" data-v-4b73742e data-v-0cb1c278 data-v-0cb1c278><div class="loader-main" data-v-4b73742e><div data-v-4b73742e></div><div data-v-4b73742e></div><div data-v-4b73742e></div><div data-v-4b73742e></div></div> <!----> <!----></div> <div class="password-shadow password-wrapper-out" style="display:none;" data-v-da0660ba data-v-0cb1c278 data-v-0cb1c278><h3 class="title" style="display:none;" data-v-da0660ba data-v-da0660ba>KnightSama‘s Blog</h3> <!----> <label id="box" class="inputBox" style="display:none;" data-v-da0660ba data-v-da0660ba><input type="password" value="" data-v-da0660ba> <span data-v-da0660ba>Konck! Knock!</span> <button data-v-da0660ba>OK</button></label> <div class="footer" style="display:none;" data-v-da0660ba data-v-da0660ba><span data-v-da0660ba><i class="iconfont reco-theme" data-v-da0660ba></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-da0660ba>vuePress-theme-reco</a></span> <span data-v-da0660ba><i class="iconfont reco-copyright" data-v-da0660ba></i> <a data-v-da0660ba><span data-v-da0660ba>KnightSama</span>
            
          <!---->
          2021
        </a></span></div></div> <div class="hide" data-v-0cb1c278><header class="navbar" data-v-0cb1c278><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/Misaka.jpg" alt="KnightSama‘s Blog" class="logo"> <span class="site-name">KnightSama‘s Blog</span></a> <div class="links"><div class="color-picker"><a class="color-button"><i class="iconfont reco-color"></i></a> <div class="color-picker-menu" style="display:none;"><div class="mode-options"><h4 class="title">Choose mode</h4> <ul class="color-mode-options"><li class="dark">dark</li><li class="auto active">auto</li><li class="light">light</li></ul></div></div></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  首页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/iOS/" class="nav-link"><i class="iconfont undefined"></i>
  iOS
</a></li><li class="dropdown-item"><!----> <a href="/categories/集锦/" class="nav-link"><i class="iconfont undefined"></i>
  集锦
</a></li><li class="dropdown-item"><!----> <a href="/categories/JavaScript/" class="nav-link"><i class="iconfont undefined"></i>
  JavaScript
</a></li><li class="dropdown-item"><!----> <a href="/categories/Github/" class="nav-link"><i class="iconfont undefined"></i>
  Github
</a></li><li class="dropdown-item"><!----> <a href="/categories/Python/" class="nav-link"><i class="iconfont undefined"></i>
  Python
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间线
</a></div><div class="nav-item"><a href="https://github.com/KnightSama" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  GitHub
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div> <!----></nav></div></header> <div class="sidebar-mask" data-v-0cb1c278></div> <aside class="sidebar" data-v-0cb1c278><div class="personal-info-wrapper" data-v-4a3def3c><img src="/Misaka.jpg" alt="author-avatar" class="personal-img" data-v-4a3def3c> <h3 class="name" data-v-4a3def3c>
    KnightSama
  </h3> <div class="num" data-v-4a3def3c><div data-v-4a3def3c><h3 data-v-4a3def3c>27</h3> <h6 data-v-4a3def3c>文章</h6></div> <div data-v-4a3def3c><h3 data-v-4a3def3c>14</h3> <h6 data-v-4a3def3c>标签</h6></div></div> <hr data-v-4a3def3c></div> <nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  首页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/iOS/" class="nav-link"><i class="iconfont undefined"></i>
  iOS
</a></li><li class="dropdown-item"><!----> <a href="/categories/集锦/" class="nav-link"><i class="iconfont undefined"></i>
  集锦
</a></li><li class="dropdown-item"><!----> <a href="/categories/JavaScript/" class="nav-link"><i class="iconfont undefined"></i>
  JavaScript
</a></li><li class="dropdown-item"><!----> <a href="/categories/Github/" class="nav-link"><i class="iconfont undefined"></i>
  Github
</a></li><li class="dropdown-item"><!----> <a href="/categories/Python/" class="nav-link"><i class="iconfont undefined"></i>
  Python
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间线
</a></div><div class="nav-item"><a href="https://github.com/KnightSama" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  GitHub
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>使用 GCD 并发编程</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/Views/20160611.html#队列-dispatch-queue" class="sidebar-link">队列(Dispatch Queue)</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/Views/20160611.html#串行队列" class="sidebar-link">串行队列</a></li><li class="sidebar-sub-header"><a href="/Views/20160611.html#并行队列" class="sidebar-link">并行队列</a></li><li class="sidebar-sub-header"><a href="/Views/20160611.html#全局队列" class="sidebar-link">全局队列</a></li><li class="sidebar-sub-header"><a href="/Views/20160611.html#主队列" class="sidebar-link">主队列</a></li><li class="sidebar-sub-header"><a href="/Views/20160611.html#队列优先级" class="sidebar-link">队列优先级</a></li></ul></li><li><a href="/Views/20160611.html#任务" class="sidebar-link">任务</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/Views/20160611.html#同步任务" class="sidebar-link">同步任务</a></li><li class="sidebar-sub-header"><a href="/Views/20160611.html#异步任务" class="sidebar-link">异步任务</a></li></ul></li><li><a href="/Views/20160611.html#组-dispatch-group" class="sidebar-link">组(Dispatch Group)</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/Views/20160611.html#创建组" class="sidebar-link">创建组</a></li><li class="sidebar-sub-header"><a href="/Views/20160611.html#将队列添加到组中" class="sidebar-link">将队列添加到组中</a></li><li class="sidebar-sub-header"><a href="/Views/20160611.html#通过-enter、leave-将队列添加到组中" class="sidebar-link">通过 enter、leave 将队列添加到组中</a></li><li class="sidebar-sub-header"><a href="/Views/20160611.html#组内任务完成后通知" class="sidebar-link">组内任务完成后通知</a></li></ul></li><li><a href="/Views/20160611.html#gcd常见用法和应用场景" class="sidebar-link">GCD常见用法和应用场景</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/Views/20160611.html#延迟执行" class="sidebar-link">延迟执行</a></li><li class="sidebar-sub-header"><a href="/Views/20160611.html#方法只执行一次" class="sidebar-link">方法只执行一次</a></li><li class="sidebar-sub-header"><a href="/Views/20160611.html#dispatch-barrier" class="sidebar-link">Dispatch_barrier</a></li><li class="sidebar-sub-header"><a href="/Views/20160611.html#dispatch-apply" class="sidebar-link">Dispatch_apply</a></li><li class="sidebar-sub-header"><a href="/Views/20160611.html#队列挂起与恢复" class="sidebar-link">队列挂起与恢复</a></li><li class="sidebar-sub-header"><a href="/Views/20160611.html#定时器" class="sidebar-link">定时器</a></li><li class="sidebar-sub-header"><a href="/Views/20160611.html#信号量-semaphore" class="sidebar-link">信号量 Semaphore</a></li><li class="sidebar-sub-header"><a href="/Views/20160611.html#dispatch-i-o" class="sidebar-link">Dispatch I/O</a></li></ul></li></ul></section></li></ul> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-da0660ba data-v-0cb1c278><h3 class="title" style="display:none;" data-v-da0660ba data-v-da0660ba>使用 GCD 并发编程</h3> <!----> <label id="box" class="inputBox" style="display:none;" data-v-da0660ba data-v-da0660ba><input type="password" value="" data-v-da0660ba> <span data-v-da0660ba>Konck! Knock!</span> <button data-v-da0660ba>OK</button></label> <div class="footer" style="display:none;" data-v-da0660ba data-v-da0660ba><span data-v-da0660ba><i class="iconfont reco-theme" data-v-da0660ba></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-da0660ba>vuePress-theme-reco</a></span> <span data-v-da0660ba><i class="iconfont reco-copyright" data-v-da0660ba></i> <a data-v-da0660ba><span data-v-da0660ba>KnightSama</span>
            
          <!---->
          2021
        </a></span></div></div> <div data-v-0cb1c278><main class="page"><div class="sidebar-open-close"><svg xmlns="http://www.w3.org/2000/svg" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <!----> <div class="page-title" style="display:none;"><h1>使用 GCD 并发编程</h1> <hr> <div data-v-73367bb4><i class="iconfont reco-account" data-v-73367bb4><span data-v-73367bb4>KnightSama</span></i> <i class="iconfont reco-date" data-v-73367bb4><span data-v-73367bb4>2016-06-11</span></i> <i class="iconfont reco-eye" data-v-73367bb4><span id="/Views/20160611.html" data-flag-title="Your Article Title" class="leancloud-visitors" data-v-73367bb4><a class="leancloud-visitors-count" style="font-size:.9rem;font-weight:normal;color:#999;"></a></span></i> <i class="iconfont reco-tag tags" data-v-73367bb4><span class="tag-item" data-v-73367bb4>
      iOS
    </span><span class="tag-item" data-v-73367bb4>
      Objective-C
    </span><span class="tag-item" data-v-73367bb4>
      Swift
    </span></i></div></div> <div class="theme-reco-content content__default" style="display:none;"><p>GCD 是一套底层级的 C API，通过 GCD 可以方便的进行并发编程。GCD 在后端管理着一个线程池，它决定代码块在哪个线程执行，并根据可用的系统资源对这些线程进行管理。
</p> <h2 id="队列-dispatch-queue"><a href="#队列-dispatch-queue" class="header-anchor">#</a> 队列 (Dispatch Queue)</h2> <p>队列是一个对象，它会以先进先出的方式管理您提交的任务。</p> <h3 id="串行队列"><a href="#串行队列" class="header-anchor">#</a> 串行队列</h3> <p>串行队列将任务以先进先出 (FIFO) 的顺序来执行。多个串行队列之间是并发执行的。如果你创建了 4 个串行队列，每一个队列在同一时间都只执行一个任务，对这四个任务来说，他们是相互独立且并发执行的。</p> <div class="tabs-component"><ul role="tablist" class="tabs-component-tabs"></ul> <div class="tabs-component-panels"><section aria-hidden="true" id="first-tab" role="tabpanel" class="tabs-component-panel" style="display:none;"><p>在 Objective-C 中我们可以通过 <code>dispatch_queue_create</code>  并指定队列类型为 <code>DISPATCH_QUEUE_SERIAL</code>  来创建串行队列，若没有指定队列类型则默认为串行队列</p> <div class="language-objectivec line-numbers-mode"><pre class="language-objectivec"><code><span class="token comment">// 未指定type则默认为串行队列</span>
dispatch_queue_t queue <span class="token operator">=</span> <span class="token function">dispatch_queue_create</span><span class="token punctuation">(</span><span class="token string">&quot;queue&quot;</span><span class="token punctuation">,</span> <span class="token constant">NULL</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 指定type为串行队列</span>
dispatch_queue_t queue <span class="token operator">=</span> <span class="token function">dispatch_queue_create</span><span class="token punctuation">(</span><span class="token string">&quot;queue&quot;</span><span class="token punctuation">,</span> DISPATCH_QUEUE_SERIAL<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div></section> <section aria-hidden="true" id="second-tab" role="tabpanel" class="tabs-component-panel" style="display:none;"><p>在 Swift 中我们可以通过  <code>DispatchQueue</code>  来创建串行队列</p> <div class="language-swift line-numbers-mode"><pre class="language-swift"><code><span class="token keyword">let</span> queue <span class="token operator">=</span> <span class="token function">DispatchQueue</span><span class="token punctuation">(</span>label<span class="token punctuation">:</span> <span class="token string">&quot;queue&quot;</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></section></div></div> <h3 id="并行队列"><a href="#并行队列" class="header-anchor">#</a> 并行队列</h3> <p>并行队列虽然能同时执行多个任务，但这些任务仍然是按照先到先执行 (FIFO) 的顺序来执行的。并行队列会基于系统负载来合适地选择并发执行这些任务。</p> <div class="tabs-component"><ul role="tablist" class="tabs-component-tabs"></ul> <div class="tabs-component-panels"><section aria-hidden="true" id="first-tab" role="tabpanel" class="tabs-component-panel" style="display:none;"><p>在 Objective-C 中我们可以通过指定队列类型为 <code>DISPATCH_QUEUE_CONCURRENT</code>  来创建并行队列</p> <div class="language-objectivec line-numbers-mode"><pre class="language-objectivec"><code>dispatch_queue_t queue <span class="token operator">=</span> <span class="token function">dispatch_queue_create</span><span class="token punctuation">(</span><span class="token string">&quot;queue&quot;</span><span class="token punctuation">,</span> DISPATCH_QUEUE_CONCURRENT<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></section> <section aria-hidden="true" id="second-tab" role="tabpanel" class="tabs-component-panel" style="display:none;"><p>在 Swift 中我们可以通过设置 <code>attributes</code>  参数为 <code>.concurrent</code>  来创建并行队列</p> <div class="language-swift line-numbers-mode"><pre class="language-swift"><code><span class="token keyword">let</span> queue <span class="token operator">=</span> <span class="token function">DispatchQueue</span><span class="token punctuation">(</span>label<span class="token punctuation">:</span> <span class="token string">&quot;queue&quot;</span><span class="token punctuation">,</span> attributes<span class="token punctuation">:</span> <span class="token punctuation">.</span>concurrent<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></section></div></div> <h3 id="全局队列"><a href="#全局队列" class="header-anchor">#</a> 全局队列</h3> <p>全局队列是系统提供的并行队列，与上面我们自己创建的队列不同，全局队列没有名字，无需创建即可使用</p> <div class="tabs-component"><ul role="tablist" class="tabs-component-tabs"></ul> <div class="tabs-component-panels"><section aria-hidden="true" id="first-tab" role="tabpanel" class="tabs-component-panel" style="display:none;"><p>在 Objective-C 中我们可以通过 <code>dispatch_get_global_queue</code>  获得全局队列</p> <div class="language-objectivec line-numbers-mode"><pre class="language-objectivec"><code>dispatch_queue_t queue <span class="token operator">=</span> <span class="token function">dispatch_get_global_queue</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p><code>dispatch_get_global_queue</code>  包含了两个参数，第一个参数是<a href="#%E9%98%9F%E5%88%97%E4%BC%98%E5%85%88%E7%BA%A7">优先级</a> (参见<a href="#%E9%98%9F%E5%88%97%E4%BC%98%E5%85%88%E7%BA%A7">队列优先级</a>)；第二个参数是系统保留参数，始终为 0</p></section> <section aria-hidden="true" id="second-tab" role="tabpanel" class="tabs-component-panel" style="display:none;"><p>在 Swift 中我们可以通过 <code>global</code>  获取全局队列</p> <div class="language-swift line-numbers-mode"><pre class="language-swift"><code><span class="token keyword">let</span> queue <span class="token operator">=</span> <span class="token builtin">DispatchQueue</span><span class="token punctuation">.</span><span class="token function">global</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></section></div></div> <h3 id="主队列"><a href="#主队列" class="header-anchor">#</a> 主队列</h3> <p>主队列是一个串行队列，和其它串行队列一样，这个队列中的任务一次只能执行一个。它能保证所有的任务都在主线程执行，而主线程是唯一可用于更新 UI 的线程。</p> <div class="tabs-component"><ul role="tablist" class="tabs-component-tabs"></ul> <div class="tabs-component-panels"><section aria-hidden="true" id="first-tab" role="tabpanel" class="tabs-component-panel" style="display:none;"><p>在 Objective-C 中我们可以通过 <code>dispatch_get_main_queue</code>  来获得主队列。</p> <div class="language-objectivec line-numbers-mode"><pre class="language-objectivec"><code>dispatch_queue_t queue <span class="token operator">=</span> <span class="token function">dispatch_get_main_queue</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></section> <section aria-hidden="true" id="second-tab" role="tabpanel" class="tabs-component-panel" style="display:none;"><p>在 Swift 中我们可以通过 <code>main</code>  来获得主队列。</p> <div class="language-swift line-numbers-mode"><pre class="language-swift"><code><span class="token keyword">let</span> queue <span class="token operator">=</span> <span class="token builtin">DispatchQueue</span><span class="token punctuation">.</span>main
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></section></div></div> <h3 id="队列优先级"><a href="#队列优先级" class="header-anchor">#</a> 队列优先级</h3> <p>优先级决定了对一个任务分配资源的大小，并非绝对的执行顺序。</p> <p>下面是 8.0 之后的服务优先级参数，从上到下优先级依次降低，系统会通过合理的资源控制来最高效的执行任务代码</p> <table><thead><tr><th>优先级</th> <th>Objective-C 参数</th> <th>Swift 参数</th> <th>值</th></tr></thead> <tbody><tr><td>用户交互</td> <td>QOS_CLASS_USER_INTERACTIVE</td> <td>userInteractive</td> <td>0x21</td></tr> <tr><td>用户期望</td> <td>QOS_CLASS_USER_INITIATED</td> <td>userInitiated</td> <td>0x19</td></tr> <tr><td>默认</td> <td>QOS_CLASS_DEFAULT</td> <td>default</td> <td>0x15</td></tr> <tr><td>实用工具</td> <td>QOS_CLASS_UTILITY</td> <td>utility</td> <td>0x11</td></tr> <tr><td>后台</td> <td>QOS_CLASS_BACKGROUND</td> <td>background</td> <td>0x09</td></tr> <tr><td>未指定</td> <td>QOS_CLASS_UNSPECIFIED</td> <td>unspecified</td> <td>0x00</td></tr></tbody></table> <p>下面是 7.0 之前的优先级参数</p> <table><thead><tr><th>优先级</th> <th>参数</th> <th>值</th></tr></thead> <tbody><tr><td>高</td> <td>DISPATCH_QUEUE_PRIORITY_HIGH</td> <td>2</td></tr> <tr><td>默认</td> <td>DISPATCH_QUEUE_PRIORITY_DEFAULT</td> <td>0</td></tr> <tr><td>低</td> <td>DISPATCH_QUEUE_PRIORITY_LOW</td> <td>-2</td></tr> <tr><td>后台</td> <td>DISPATCH_QUEUE_PRIORITY_BACKGROUND</td> <td>INT16_MIN</td></tr></tbody></table> <div class="tabs-component"><ul role="tablist" class="tabs-component-tabs"></ul> <div class="tabs-component-panels"><section aria-hidden="true" id="first-tab" role="tabpanel" class="tabs-component-panel" style="display:none;"><p>在 Objective-C 中我们可以改变全局队列的优先级</p> <div class="language-objectivec line-numbers-mode"><pre class="language-objectivec"><code>dispatch_queue_t queue <span class="token operator">=</span> <span class="token function">dispatch_get_global_queue</span><span class="token punctuation">(</span>DISPATCH_QUEUE_PRIORITY_HIGH<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></section> <section aria-hidden="true" id="second-tab" role="tabpanel" class="tabs-component-panel" style="display:none;"><p>在 Swift 中我们可以在创建队列时或在全局队列中设置 <code>qos</code>  参数来设置优先级</p> <div class="language-swift line-numbers-mode"><pre class="language-swift"><code><span class="token comment">// 创建队列时设置优先级</span>
<span class="token keyword">let</span> queue <span class="token operator">=</span> <span class="token function">DispatchQueue</span><span class="token punctuation">(</span>label<span class="token punctuation">:</span> <span class="token string">&quot;queue&quot;</span><span class="token punctuation">,</span> qos<span class="token punctuation">:</span> <span class="token punctuation">.</span>userInteractive<span class="token punctuation">)</span>

<span class="token comment">// 变更全局队列优先级</span>
<span class="token keyword">let</span> queue <span class="token operator">=</span> <span class="token builtin">DispatchQueue</span><span class="token punctuation">.</span><span class="token function">global</span><span class="token punctuation">(</span>qos<span class="token punctuation">:</span> <span class="token punctuation">.</span><span class="token keyword">default</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div></section></div></div> <h2 id="任务"><a href="#任务" class="header-anchor">#</a> 任务</h2> <p>GCD 中的任务只是一个代码块，它可以指一个 block 或者函数指针。根据这个代码块添加进入队列的方式，将任务分为同步任务和异步任务：</p> <h3 id="同步任务"><a href="#同步任务" class="header-anchor">#</a> 同步任务</h3> <p>加入队列的同步任务会被顺序执行</p> <div class="tabs-component"><ul role="tablist" class="tabs-component-tabs"></ul> <div class="tabs-component-panels"><section aria-hidden="true" id="first-tab" role="tabpanel" class="tabs-component-panel" style="display:none;"><p>在 Objective-C 中我们通过  <code>dispatch_sync</code>  来将同步任务添加到队列中</p> <div class="language-objectivec line-numbers-mode"><pre class="language-objectivec"><code><span class="token comment">// 队列中添加同步任务</span>
<span class="token function">dispatch_sync</span><span class="token punctuation">(</span><span class="token function">dispatch_queue_create</span><span class="token punctuation">(</span><span class="token string">&quot;queue&quot;</span><span class="token punctuation">,</span> <span class="token constant">NULL</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token operator">^</span><span class="token punctuation">{</span>
    <span class="token comment">// 任务</span>
    <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div></section> <section aria-hidden="true" id="second-tab" role="tabpanel" class="tabs-component-panel" style="display:none;"><p>在 Swift 中我们通过在队列后调用  <code>sync</code>  来将同步任务添加到队列中</p> <div class="language-swift line-numbers-mode"><pre class="language-swift"><code><span class="token comment">// 队列中添加同步任务</span>
<span class="token function">DispatchQueue</span><span class="token punctuation">(</span>label<span class="token punctuation">:</span> <span class="token string">&quot;queue&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span>sync <span class="token punctuation">{</span>
    <span class="token comment">// 任务</span>
    <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div></section></div></div> <div class="custom-block tip"><p>将同步任务加入串行队列，队列中的任务会顺序执行，注意在一个任务未结束时调起其它同步任务会死锁。<br>
将同步任务加入并行队列，队列中的任务会顺序执行，没有什么用。</p></div> <div class="custom-block danger"><p class="custom-block-title">注意</p> <p>如果在一个串行队列 (如主队列) Q 上的同步任务 A 中再次添加一个同步任务 B，并且指定 B 的队列也为串行队列 Q，此时任务 B 被放到队列的队尾等待执行，因为这是一个串行队列，所以任务 A 会等待任务 B 先执行，而这个任务永远没有机会执行，此时队列被永远阻塞造成死锁。 (这就是在主线程调用 <code>dispatch_sync</code>  函数，并且在 <code>dispatch_sync</code>  函数中传入 <code>main_queue</code>  作为队列造成死锁的情况)</p> <div class="language-objectivec line-numbers-mode"><pre class="language-objectivec"><code><span class="token comment">// 在主线程调用</span>
<span class="token function">dispatch_sync</span><span class="token punctuation">(</span><span class="token function">dispatch_get_main_queue</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token operator">^</span><span class="token punctuation">{</span>
    <span class="token comment">// 死锁</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div></div> <h3 id="异步任务"><a href="#异步任务" class="header-anchor">#</a> 异步任务</h3> <div class="tabs-component"><ul role="tablist" class="tabs-component-tabs"></ul> <div class="tabs-component-panels"><section aria-hidden="true" id="first-tab" role="tabpanel" class="tabs-component-panel" style="display:none;"><p>在 Objective-C 中我们通过  <code>dispatch_async</code>  来将异步任务添加到队列中</p> <div class="language-objectivec line-numbers-mode"><pre class="language-objectivec"><code><span class="token comment">// 队列中添加异步任务</span>
<span class="token function">dispatch_async</span><span class="token punctuation">(</span><span class="token function">dispatch_queue_create</span><span class="token punctuation">(</span><span class="token string">&quot;queue&quot;</span><span class="token punctuation">,</span> <span class="token constant">NULL</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token operator">^</span><span class="token punctuation">{</span>
    <span class="token comment">// 任务</span>
    <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div></section> <section aria-hidden="true" id="second-tab" role="tabpanel" class="tabs-component-panel" style="display:none;"><p>在 Swift 中我们通过在队列后调用  <code>async</code>  来将异步任务添加到队列中</p> <div class="language-swift line-numbers-mode"><pre class="language-swift"><code><span class="token comment">// 队列中添加异步任务</span>
<span class="token function">DispatchQueue</span><span class="token punctuation">(</span>label<span class="token punctuation">:</span> <span class="token string">&quot;queue&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span>async <span class="token punctuation">{</span>
    <span class="token comment">// 任务</span>
    <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div></section></div></div> <div class="custom-block tip"><p>将异步任务加入串行队列，整个队列并行执行，队列中的任务会顺序执行，并且不会出现死锁问题。<br>
将异步任务加入并行队列，会并行执行多个任务，这也是我们最常用的一种方式。</p></div> <h2 id="组-dispatch-group"><a href="#组-dispatch-group" class="header-anchor">#</a> 组 (Dispatch Group)</h2> <p>我们可以将多个队列添加到一个组中，在这多个任务完成后我们可以获得通知</p> <h3 id="创建组"><a href="#创建组" class="header-anchor">#</a> 创建组</h3> <div class="tabs-component"><ul role="tablist" class="tabs-component-tabs"></ul> <div class="tabs-component-panels"><section aria-hidden="true" id="first-tab" role="tabpanel" class="tabs-component-panel" style="display:none;"><p>在 Objective-C 中我们通过  <code>dispatch_group_create</code>  来创建组</p> <div class="language-objectivec line-numbers-mode"><pre class="language-objectivec"><code>dispatch_group_t group <span class="token operator">=</span> <span class="token function">dispatch_group_create</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></section> <section aria-hidden="true" id="second-tab" role="tabpanel" class="tabs-component-panel" style="display:none;"><p>在 Swift 中我们通过  <code>DispatchGroup</code>  来创建组</p> <div class="language-swift line-numbers-mode"><pre class="language-swift"><code><span class="token keyword">let</span> group <span class="token operator">=</span> <span class="token function">DispatchGroup</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></section></div></div> <h3 id="将队列添加到组中"><a href="#将队列添加到组中" class="header-anchor">#</a> 将队列添加到组中</h3> <div class="tabs-component"><ul role="tablist" class="tabs-component-tabs"></ul> <div class="tabs-component-panels"><section aria-hidden="true" id="first-tab" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-objectivec line-numbers-mode"><pre class="language-objectivec"><code><span class="token comment">// 创建组</span>
dispatch_group_t group <span class="token operator">=</span> <span class="token function">dispatch_group_create</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 将队列任务添加到组</span>
<span class="token function">dispatch_group_async</span><span class="token punctuation">(</span>group<span class="token punctuation">,</span> <span class="token function">dispatch_get_global_queue</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token operator">^</span><span class="token punctuation">{</span>
    <span class="token comment">// 任务</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div></section> <section aria-hidden="true" id="second-tab" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-swift line-numbers-mode"><pre class="language-swift"><code><span class="token comment">// 创建组</span>
<span class="token keyword">let</span> group <span class="token operator">=</span> <span class="token function">DispatchGroup</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token comment">// 将队列任务添加到组</span>
<span class="token builtin">DispatchQueue</span><span class="token punctuation">.</span><span class="token function">global</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">async</span><span class="token punctuation">(</span>group<span class="token punctuation">:</span> group<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// 任务</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div></section></div></div> <h3 id="通过-enter、leave-将队列添加到组中"><a href="#通过-enter、leave-将队列添加到组中" class="header-anchor">#</a> 通过  <code>enter</code> 、 <code>leave</code>  将队列添加到组中</h3> <p><code>enter</code>  和 <code>leave</code>  这两个函数必须成对出现，否则这一组任务就永远执行不完。</p> <div class="tabs-component"><ul role="tablist" class="tabs-component-tabs"></ul> <div class="tabs-component-panels"><section aria-hidden="true" id="first-tab" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-objectivec line-numbers-mode"><pre class="language-objectivec"><code><span class="token comment">// 创建组</span>
dispatch_group_t group <span class="token operator">=</span> <span class="token function">dispatch_group_create</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 进入组</span>
<span class="token function">dispatch_group_enter</span><span class="token punctuation">(</span>group<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 创建任务</span>
<span class="token function">dispatch_async</span><span class="token punctuation">(</span><span class="token function">dispatch_get_global_queue</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token operator">^</span><span class="token punctuation">{</span>
    <span class="token comment">// 任务</span>
    <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
    <span class="token comment">// 退出组</span>
    <span class="token function">dispatch_group_leave</span><span class="token punctuation">(</span>group<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div></section> <section aria-hidden="true" id="second-tab" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-swift line-numbers-mode"><pre class="language-swift"><code><span class="token comment">// 创建组</span>
<span class="token keyword">let</span> group <span class="token operator">=</span> <span class="token function">DispatchGroup</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token comment">// 进入组</span>
group<span class="token punctuation">.</span><span class="token function">enter</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token comment">// 创建任务</span>
<span class="token builtin">DispatchQueue</span><span class="token punctuation">.</span><span class="token function">global</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>async <span class="token punctuation">{</span>
    <span class="token comment">// 任务</span>
    <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
    <span class="token comment">// 退出组</span>
    group<span class="token punctuation">.</span><span class="token function">leave</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div></section></div></div> <h3 id="组内任务完成后通知"><a href="#组内任务完成后通知" class="header-anchor">#</a> 组内任务完成后通知</h3> <div class="tabs-component"><ul role="tablist" class="tabs-component-tabs"></ul> <div class="tabs-component-panels"><section aria-hidden="true" id="first-tab" role="tabpanel" class="tabs-component-panel" style="display:none;"><p>在 Objective-C 中 <code>dispatch_group_notify</code>  可以在组内任务全部完成后执行方法</p> <div class="language-objectivec line-numbers-mode"><pre class="language-objectivec"><code><span class="token comment">// 创建组</span>
dispatch_group_t group <span class="token operator">=</span> <span class="token function">dispatch_group_create</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 将队列任务添加到组</span>
<span class="token function">dispatch_group_async</span><span class="token punctuation">(</span>group<span class="token punctuation">,</span> <span class="token function">dispatch_get_global_queue</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token operator">^</span><span class="token punctuation">{</span>
    <span class="token comment">// 任务1</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">dispatch_group_async</span><span class="token punctuation">(</span>group<span class="token punctuation">,</span> <span class="token function">dispatch_get_global_queue</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token operator">^</span><span class="token punctuation">{</span>
    <span class="token comment">// 任务2</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 等待group中多个任务执行完毕，做一些事情</span>
<span class="token function">dispatch_group_notify</span><span class="token punctuation">(</span>group<span class="token punctuation">,</span> mainQueue<span class="token punctuation">,</span> <span class="token operator">^</span><span class="token punctuation">{</span>
    <span class="token comment">// 任务完成后，在主队列中做一些操作</span>
    <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div></section> <section aria-hidden="true" id="second-tab" role="tabpanel" class="tabs-component-panel" style="display:none;"><p>在 Swift 中 <code>notify</code>  可以在组内任务全部完成后执行方法</p> <div class="language-swift line-numbers-mode"><pre class="language-swift"><code><span class="token comment">// 创建组</span>
<span class="token keyword">let</span> group <span class="token operator">=</span> <span class="token function">DispatchGroup</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token comment">// 将队列任务添加到组</span>
<span class="token builtin">DispatchQueue</span><span class="token punctuation">.</span><span class="token function">global</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">async</span><span class="token punctuation">(</span>group<span class="token punctuation">:</span> group<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// 任务1</span>
<span class="token punctuation">}</span>
<span class="token builtin">DispatchQueue</span><span class="token punctuation">.</span><span class="token function">global</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">async</span><span class="token punctuation">(</span>group<span class="token punctuation">:</span> group<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// 任务2</span>
<span class="token punctuation">}</span>
<span class="token comment">// 等待 group 中多个任务执行完毕，做一些事情</span>
group<span class="token punctuation">.</span><span class="token function">notify</span><span class="token punctuation">(</span>queue<span class="token punctuation">:</span> <span class="token punctuation">.</span>main<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// 任务完成后，在主队列中做一些操作</span>
    <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div></section></div></div> <h2 id="gcd常见用法和应用场景"><a href="#gcd常见用法和应用场景" class="header-anchor">#</a> GCD 常见用法和应用场景</h2> <h3 id="延迟执行"><a href="#延迟执行" class="header-anchor">#</a> 延迟执行</h3> <div class="tabs-component"><ul role="tablist" class="tabs-component-tabs"></ul> <div class="tabs-component-panels"><section aria-hidden="true" id="first-tab" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-objectivec line-numbers-mode"><pre class="language-objectivec"><code>dispatch_queue_t queue<span class="token operator">=</span> <span class="token function">dispatch_get_main_queue</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">dispatch_after</span><span class="token punctuation">(</span><span class="token function">dispatch_time</span><span class="token punctuation">(</span>DISPATCH_TIME_NOW<span class="token punctuation">,</span> <span class="token punctuation">(</span>int64_t<span class="token punctuation">)</span><span class="token punctuation">(</span><span class="token number">5.0</span> <span class="token operator">*</span> NSEC_PER_SEC<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">,</span> queue<span class="token punctuation">,</span> <span class="token operator">^</span><span class="token punctuation">{</span>
    <span class="token comment">// 在queue里面延迟 5s 执行任务</span>
    <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div></section> <section aria-hidden="true" id="second-tab" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-swift line-numbers-mode"><pre class="language-swift"><code><span class="token builtin">DispatchQueue</span><span class="token punctuation">.</span>main<span class="token punctuation">.</span><span class="token function">asyncAfter</span><span class="token punctuation">(</span>deadline<span class="token punctuation">:</span> <span class="token punctuation">.</span><span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">5</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// 在queue里面延迟 5s 执行任务</span>
    <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div></section></div></div> <h3 id="方法只执行一次"><a href="#方法只执行一次" class="header-anchor">#</a> 方法只执行一次</h3> <p>Swift 不支持该方法，需要通过其他方式实现<a href="/Views/20170515.html#让方法只调用一次">前往实现方式</a></p> <div class="language-objectivec line-numbers-mode"><pre class="language-objectivec"><code><span class="token keyword">static</span> dispatch_once_t onceToken<span class="token punctuation">;</span>
<span class="token function">dispatch_once</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>onceToken<span class="token punctuation">,</span> <span class="token operator">^</span><span class="token punctuation">{</span>
    <span class="token comment">// 只执行一次的任务</span>
    <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h3 id="dispatch-barrier"><a href="#dispatch-barrier" class="header-anchor">#</a> Dispatch_barrier</h3> <p><code>Dispatch_barrier</code>  的作用可以用一个词概括－－承上启下，它保证此前的任务都先于自己执行，此后的任务也迟于自己执行。本例中，任务 2 会在任务 1 执行完之后执行，而任务 3 会等待任务 2 执行完后执行。</p> <div class="tabs-component"><ul role="tablist" class="tabs-component-tabs"></ul> <div class="tabs-component-panels"><section aria-hidden="true" id="first-tab" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-objectivec line-numbers-mode"><pre class="language-objectivec"><code><span class="token comment">// 创建队列</span>
dispatch_queue_t queue <span class="token operator">=</span> <span class="token function">dispatch_queue_create</span><span class="token punctuation">(</span><span class="token string">&quot;queue&quot;</span><span class="token punctuation">,</span> DISPATCH_QUEUE_CONCURRENT<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">dispatch_async</span><span class="token punctuation">(</span>queue<span class="token punctuation">,</span> <span class="token operator">^</span><span class="token punctuation">{</span>
    <span class="token comment">// 任务1</span>
    <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">dispatch_barrier_async</span><span class="token punctuation">(</span>queue<span class="token punctuation">,</span> <span class="token operator">^</span><span class="token punctuation">{</span>
    <span class="token comment">// 任务2</span>
    <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">dispatch_async</span><span class="token punctuation">(</span>queue<span class="token punctuation">,</span> <span class="token operator">^</span><span class="token punctuation">{</span>
    <span class="token comment">// 任务3</span>
    <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div></section> <section aria-hidden="true" id="second-tab" role="tabpanel" class="tabs-component-panel" style="display:none;"><p>Swift 需要通过  <code>flags</code>  参数来实现</p> <div class="language-swift line-numbers-mode"><pre class="language-swift"><code><span class="token comment">// 创建队列</span>
<span class="token keyword">let</span> queue <span class="token operator">=</span> <span class="token function">DispatchQueue</span><span class="token punctuation">(</span>label<span class="token punctuation">:</span> <span class="token string">&quot;queue&quot;</span><span class="token punctuation">,</span> attributes<span class="token punctuation">:</span> <span class="token punctuation">.</span>concurrent<span class="token punctuation">)</span>
queue<span class="token punctuation">.</span>async <span class="token punctuation">{</span>
    <span class="token comment">// 任务1</span>
    <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
<span class="token punctuation">}</span>
queue<span class="token punctuation">.</span><span class="token function">async</span><span class="token punctuation">(</span>flags<span class="token punctuation">:</span> <span class="token punctuation">.</span>barrier<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// 任务2</span>
    <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
<span class="token punctuation">}</span>
queue<span class="token punctuation">.</span>async <span class="token punctuation">{</span>
    <span class="token comment">// 任务3</span>
    <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div></section></div></div> <div class="custom-block danger"><p class="custom-block-title">注意</p> <p><code>Dispatch_barrier</code> <font color="red">只在自己创建的并发队列上有效</font>，在全局并发队列、串行队列上无效</p></div> <h3 id="dispatch-apply"><a href="#dispatch-apply" class="header-anchor">#</a> Dispatch_apply</h3> <p><code>Dispatch_apply</code>  可以按指定的次数将指定的任务追加到指定的队列中，因为 <code>Dispatch_apply</code>  并行的运行机制，效率一般快于 for 循环（在 for 一次循环中的处理任务很多时差距比较大）。比如这可以用来拉取网络数据后提前算出各个控件的大小，防止绘制时计算，提高表单滑动流畅性。当需要循环处理一些事情且处理顺序对结果无关时可以考虑使用该方式</p> <div class="tabs-component"><ul role="tablist" class="tabs-component-tabs"></ul> <div class="tabs-component-panels"><section aria-hidden="true" id="first-tab" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-objectivec line-numbers-mode"><pre class="language-objectivec"><code><span class="token function">dispatch_apply</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span> <span class="token function">dispatch_get_global_queue</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token operator">^</span><span class="token punctuation">(</span>size_t index<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// 循环 10 次</span>
    <span class="token function">NSLog</span><span class="token punctuation">(</span><span class="token string">@&quot;%zu&quot;</span><span class="token punctuation">,</span> index<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div></section> <section aria-hidden="true" id="second-tab" role="tabpanel" class="tabs-component-panel" style="display:none;"><p>Swift 中使用  <code>concurrentPerform</code></p> <div class="language-swift line-numbers-mode"><pre class="language-swift"><code><span class="token builtin">DispatchQueue</span><span class="token punctuation">.</span><span class="token function">concurrentPerform</span><span class="token punctuation">(</span>iterations<span class="token punctuation">:</span> <span class="token number">10</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> index <span class="token keyword">in</span>
    <span class="token comment">// 循环 10 次</span>
    <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">&quot;<span class="token interpolation"><span class="token delimiter variable">\(</span>index<span class="token delimiter variable">)</span></span>&quot;</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div></section></div></div> <h3 id="队列挂起与恢复"><a href="#队列挂起与恢复" class="header-anchor">#</a> 队列挂起与恢复</h3> <div class="tabs-component"><ul role="tablist" class="tabs-component-tabs"></ul> <div class="tabs-component-panels"><section aria-hidden="true" id="first-tab" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-objectivec line-numbers-mode"><pre class="language-objectivec"><code>dispatch_queue_t queue <span class="token operator">=</span> <span class="token function">dispatch_queue_create</span><span class="token punctuation">(</span><span class="token string">&quot;queue&quot;</span><span class="token punctuation">,</span> DISPATCH_QUEUE_CONCURRENT<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">dispatch_suspend</span><span class="token punctuation">(</span>queue<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//暂停队列queue</span>
<span class="token function">dispatch_resume</span><span class="token punctuation">(</span>queue<span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment">//恢复队列queue</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div></section> <section aria-hidden="true" id="second-tab" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-swift line-numbers-mode"><pre class="language-swift"><code><span class="token keyword">let</span> queue <span class="token operator">=</span> <span class="token function">DispatchQueue</span><span class="token punctuation">(</span>label<span class="token punctuation">:</span> <span class="token string">&quot;queue&quot;</span><span class="token punctuation">,</span> attributes<span class="token punctuation">:</span> <span class="token punctuation">.</span>concurrent<span class="token punctuation">)</span>
queue<span class="token punctuation">.</span><span class="token function">resume</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
queue<span class="token punctuation">.</span><span class="token function">suspend</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div></section></div></div> <h3 id="定时器"><a href="#定时器" class="header-anchor">#</a> 定时器</h3> <p>GCD 可以通过 Dispatch Source 来实现定时器，与 Timer 相比 GCD 定时器更加精准，并且不依赖于 Runloop</p> <div class="tabs-component"><ul role="tablist" class="tabs-component-tabs"></ul> <div class="tabs-component-panels"><section aria-hidden="true" id="first-tab" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-objectivec line-numbers-mode"><pre class="language-objectivec"><code><span class="token comment">// 在主线程创建定时器</span>
dispatch_source_t timer <span class="token operator">=</span> <span class="token function">dispatch_source_create</span><span class="token punctuation">(</span>DISPATCH_SOURCE_TYPE_TIMER<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token function">dispatch_get_main_queue</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 设置计时的时间</span>
<span class="token comment">// DISPATCH_TIME_NOW 表示从现在开始计时</span>
<span class="token comment">// 第三个参数代表间隔时间</span>
<span class="token comment">// 第四个参数是精准度(0代表绝对精准)</span>
<span class="token function">dispatch_source_set_timer</span><span class="token punctuation">(</span>timer<span class="token punctuation">,</span> DISPATCH_TIME_NOW<span class="token punctuation">,</span> <span class="token number">1.0</span> <span class="token operator">*</span> NSEC_PER_SEC<span class="token punctuation">,</span> <span class="token number">0</span> <span class="token operator">*</span> NSEC_PER_SEC<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 设置定时任务</span>
<span class="token function">dispatch_source_set_event_handler</span><span class="token punctuation">(</span>timer<span class="token punctuation">,</span> <span class="token operator">^</span><span class="token punctuation">{</span>
    <span class="token comment">// 定时执行任务</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 开始执行</span>
<span class="token function">dispatch_resume</span><span class="token punctuation">(</span>timer<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div></section> <section aria-hidden="true" id="second-tab" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-swift line-numbers-mode"><pre class="language-swift"><code><span class="token comment">// 在主线程创建定时器</span>
<span class="token keyword">let</span> timer <span class="token operator">=</span> <span class="token builtin">DispatchSource</span><span class="token punctuation">.</span><span class="token function">makeTimerSource</span><span class="token punctuation">(</span>queue<span class="token punctuation">:</span> <span class="token builtin">DispatchQueue</span><span class="token punctuation">.</span>main<span class="token punctuation">)</span>
<span class="token comment">// 设置计时的时间</span>
timer<span class="token punctuation">.</span><span class="token function">schedule</span><span class="token punctuation">(</span>deadline<span class="token punctuation">:</span> <span class="token punctuation">.</span><span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> repeating<span class="token punctuation">:</span> <span class="token punctuation">.</span><span class="token function">seconds</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span> leeway<span class="token punctuation">:</span> <span class="token punctuation">.</span><span class="token function">seconds</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token comment">// 设置定时任务</span>
timer<span class="token punctuation">.</span>setEventHandler <span class="token punctuation">{</span>
    <span class="token comment">// 定时执行任务</span>
<span class="token punctuation">}</span>
<span class="token comment">// 开始执行</span>
timer<span class="token punctuation">.</span><span class="token function">resume</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div></section></div></div> <div class="custom-block tip"><p class="custom-block-title">定时器的控制方法</p> <ul><li><code>dispatch_activate()</code>  可以在 Timer 被创建后激活 Timer。该方法在 iOS 10 版本后可用，之前的版本可以用  <code>dispatch_resume()</code>  来激活</li> <li><code>dispatch_suspend()</code>  可以让运行中的 Timer 挂起即暂停</li> <li><code>dispatch_resume()</code>  可以用来将挂起中的 Timer 唤醒来继续运行，也可以在 Timer 被创建后激活 Timer</li> <li><code>dispatch_source_cancel()</code>  可以取消 Timer，被取消的 Timer 会被销毁</li></ul></div> <div class="custom-block warning"><p class="custom-block-title">注意事项</p> <ul><li><code>dispatch_suspend()</code>  与  <code>dispatch_resume()</code>  必须成对出现，除了创建完毕第一次激活 Timer 可以先调用  <code>dispatch_resume()</code>  外，如果没有挂起就调用会导致应用崩溃</li> <li>当 Timer 处于挂起状态时，不能够将 Timer 置为 nil 或者调用  <code>dispatch_source_cancel()</code>  方法，必须先激活才能取消，否则应用会崩溃</li></ul></div> <div class="custom-block tip"><p class="custom-block-title">DispatchTime 与 DispatchWallTime</p> <p>在设置时间时有 DispatchTime 与 DispatchWallTime 两种选择，他们的主要区别是 DispatchTime 在应用进入休眠如退出到后台时会暂停而 DispatchWallTime 则会继续运行</p></div> <h3 id="信号量-semaphore"><a href="#信号量-semaphore" class="header-anchor">#</a> 信号量 Semaphore</h3> <h4 id="创建信号量"><a href="#创建信号量" class="header-anchor">#</a> 创建信号量</h4> <div class="tabs-component"><ul role="tablist" class="tabs-component-tabs"></ul> <div class="tabs-component-panels"><section aria-hidden="true" id="first-tab" role="tabpanel" class="tabs-component-panel" style="display:none;"><p>在 Objective-C 中通过  <code>dispatch_semaphore_create</code>  创建信号量</p> <div class="language-objectivec line-numbers-mode"><pre class="language-objectivec"><code><span class="token comment">// 创建信号量 设置信号量计数值为 3</span>
dispatch_semaphore_t semaphore <span class="token operator">=</span> <span class="token function">dispatch_semaphore_create</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div></section> <section aria-hidden="true" id="second-tab" role="tabpanel" class="tabs-component-panel" style="display:none;"><p>在 Swift 中通过  <code>DispatchSemaphore</code>  创建信号量</p> <div class="language-swift line-numbers-mode"><pre class="language-swift"><code><span class="token comment">// 创建信号量 设置信号量计数值为 3</span>
<span class="token keyword">let</span> semaphore <span class="token operator">=</span> <span class="token function">DispatchSemaphore</span><span class="token punctuation">(</span>value<span class="token punctuation">:</span> <span class="token number">3</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div></section></div></div> <h4 id="使用信号量"><a href="#使用信号量" class="header-anchor">#</a> 使用信号量</h4> <div class="tabs-component"><ul role="tablist" class="tabs-component-tabs"></ul> <div class="tabs-component-panels"><section aria-hidden="true" id="first-tab" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-objectivec line-numbers-mode"><pre class="language-objectivec"><code><span class="token comment">// 若 semaphore 计数为0则等待，大于0则使其减1</span>
<span class="token function">dispatch_semaphore_wait</span><span class="token punctuation">(</span>semaphore<span class="token punctuation">,</span> DISPATCH_TIME_FOREVER<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 使 semaphore 计数加1</span>
<span class="token function">dispatch_semaphore_signal</span><span class="token punctuation">(</span>semaphore<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div></section> <section aria-hidden="true" id="second-tab" role="tabpanel" class="tabs-component-panel" style="display:none;"><p>在 Swift 中通过  <code>DispatchSemaphore</code>  创建信号量</p> <div class="language-swift line-numbers-mode"><pre class="language-swift"><code><span class="token comment">// 若 semaphore 计数为0则等待，大于0则使其减1</span>
semaphore<span class="token punctuation">.</span><span class="token function">wait</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token comment">// 使 semaphore 计数加1</span>
semaphore<span class="token punctuation">.</span><span class="token function">signal</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div></section></div></div> <h4 id="解决同步问题"><a href="#解决同步问题" class="header-anchor">#</a> 解决同步问题</h4> <p>下面的输出肯定为 1、2、3。</p> <div class="tabs-component"><ul role="tablist" class="tabs-component-tabs"></ul> <div class="tabs-component-panels"><section aria-hidden="true" id="first-tab" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-objectivec line-numbers-mode"><pre class="language-objectivec"><code>dispatch_queue_t queue <span class="token operator">=</span> <span class="token function">dispatch_get_global_queue</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
dispatch_semaphore_t semaphore1 <span class="token operator">=</span> <span class="token function">dispatch_semaphore_create</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
dispatch_semaphore_t semaphore2 <span class="token operator">=</span> <span class="token function">dispatch_semaphore_create</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
dispatch_semaphore_t semaphore3 <span class="token operator">=</span> <span class="token function">dispatch_semaphore_create</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token function">dispatch_async</span><span class="token punctuation">(</span>queue<span class="token punctuation">,</span> <span class="token operator">^</span><span class="token punctuation">{</span>
    <span class="token function">dispatch_semaphore_wait</span><span class="token punctuation">(</span>semaphore1<span class="token punctuation">,</span> DISPATCH_TIME_FOREVER<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">NSLog</span><span class="token punctuation">(</span><span class="token string">@&quot;1\n&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">dispatch_semaphore_signal</span><span class="token punctuation">(</span>semaphore2<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">dispatch_semaphore_signal</span><span class="token punctuation">(</span>semaphore1<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token function">dispatch_async</span><span class="token punctuation">(</span>queue<span class="token punctuation">,</span> <span class="token operator">^</span><span class="token punctuation">{</span>
    <span class="token function">dispatch_semaphore_wait</span><span class="token punctuation">(</span>semaphore2<span class="token punctuation">,</span> DISPATCH_TIME_FOREVER<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">NSLog</span><span class="token punctuation">(</span><span class="token string">@&quot;2\n&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">dispatch_semaphore_signal</span><span class="token punctuation">(</span>semaphore3<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">dispatch_semaphore_signal</span><span class="token punctuation">(</span>semaphore2<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token function">dispatch_async</span><span class="token punctuation">(</span>queue<span class="token punctuation">,</span> <span class="token operator">^</span><span class="token punctuation">{</span>
    <span class="token function">dispatch_semaphore_wait</span><span class="token punctuation">(</span>semaphore3<span class="token punctuation">,</span> DISPATCH_TIME_FOREVER<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">NSLog</span><span class="token punctuation">(</span><span class="token string">@&quot;3\n&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">dispatch_semaphore_signal</span><span class="token punctuation">(</span>semaphore3<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br></div></div></section> <section aria-hidden="true" id="second-tab" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-swift line-numbers-mode"><pre class="language-swift"><code><span class="token keyword">let</span> queue <span class="token operator">=</span> <span class="token builtin">DispatchQueue</span><span class="token punctuation">.</span><span class="token function">global</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token keyword">let</span> semaphore1 <span class="token operator">=</span> <span class="token function">DispatchSemaphore</span><span class="token punctuation">(</span>value<span class="token punctuation">:</span> <span class="token number">1</span><span class="token punctuation">)</span>
<span class="token keyword">let</span> semaphore2 <span class="token operator">=</span> <span class="token function">DispatchSemaphore</span><span class="token punctuation">(</span>value<span class="token punctuation">:</span> <span class="token number">0</span><span class="token punctuation">)</span>
<span class="token keyword">let</span> semaphore3 <span class="token operator">=</span> <span class="token function">DispatchSemaphore</span><span class="token punctuation">(</span>value<span class="token punctuation">:</span> <span class="token number">0</span><span class="token punctuation">)</span>

queue<span class="token punctuation">.</span>async <span class="token punctuation">{</span>
    semaphore1<span class="token punctuation">.</span><span class="token function">wait</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">&quot;1&quot;</span><span class="token punctuation">)</span>
    semaphore2<span class="token punctuation">.</span><span class="token function">signal</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    semaphore1<span class="token punctuation">.</span><span class="token function">signal</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>

queue<span class="token punctuation">.</span>async <span class="token punctuation">{</span>
    semaphore2<span class="token punctuation">.</span><span class="token function">wait</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">&quot;2&quot;</span><span class="token punctuation">)</span>
    semaphore3<span class="token punctuation">.</span><span class="token function">signal</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    semaphore2<span class="token punctuation">.</span><span class="token function">signal</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>

queue<span class="token punctuation">.</span>async <span class="token punctuation">{</span>
    semaphore3<span class="token punctuation">.</span><span class="token function">wait</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">&quot;3&quot;</span><span class="token punctuation">)</span>
    semaphore3<span class="token punctuation">.</span><span class="token function">signal</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br></div></div></section></div></div> <h4 id="解决有限资源访问问题"><a href="#解决有限资源访问问题" class="header-anchor">#</a> 解决有限资源访问问题</h4> <p>for 循环看似能创建 100 个异步任务，实质由于信号限制，最多创建 10 个异步任务。</p> <div class="tabs-component"><ul role="tablist" class="tabs-component-tabs"></ul> <div class="tabs-component-panels"><section aria-hidden="true" id="first-tab" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-objectivec line-numbers-mode"><pre class="language-objectivec"><code>dispatch_semaphore_t semaphore <span class="token operator">=</span> <span class="token function">dispatch_semaphore_create</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> <span class="token number">100</span><span class="token punctuation">;</span> i <span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token function">dispatch_semaphore_wait</span><span class="token punctuation">(</span>semaphore<span class="token punctuation">,</span> DISPATCH_TIME_FOREVER<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">dispatch_async</span><span class="token punctuation">(</span><span class="token function">dispatch_get_global_queue</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token operator">^</span><span class="token punctuation">{</span>
    <span class="token comment">// 任务</span>
    <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
    <span class="token function">dispatch_semaphore_signal</span><span class="token punctuation">(</span>semaphore<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div></section> <section aria-hidden="true" id="second-tab" role="tabpanel" class="tabs-component-panel" style="display:none;"><p>在 Swift 中通过  <code>DispatchSemaphore</code>  创建信号量</p> <div class="language-swift line-numbers-mode"><pre class="language-swift"><code><span class="token keyword">let</span> semaphore <span class="token operator">=</span> <span class="token function">DispatchSemaphore</span><span class="token punctuation">(</span>value<span class="token punctuation">:</span> <span class="token number">10</span><span class="token punctuation">)</span>
<span class="token keyword">for</span> <span class="token keyword">var</span> i <span class="token keyword">in</span> <span class="token number">0</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token operator">&lt;</span> <span class="token number">100</span> <span class="token punctuation">{</span>
    semaphore<span class="token punctuation">.</span><span class="token function">wait</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token builtin">DispatchQueue</span><span class="token punctuation">.</span><span class="token function">global</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>async <span class="token punctuation">{</span>
        semaphore<span class="token punctuation">.</span><span class="token function">signal</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div></section></div></div> <h3 id="dispatch-i-o"><a href="#dispatch-i-o" class="header-anchor">#</a> Dispatch I/O</h3> <h4 id="异步串行读取文件"><a href="#异步串行读取文件" class="header-anchor">#</a> 异步串行读取文件</h4> <div class="language-objectivec line-numbers-mode"><pre class="language-objectivec"><code>NSString <span class="token operator">*</span>desktop <span class="token operator">=</span> <span class="token string">@&quot;/Users/XXX/Desktop&quot;</span><span class="token punctuation">;</span>
NSString <span class="token operator">*</span>path <span class="token operator">=</span> <span class="token punctuation">[</span>desktop stringByAppendingPathComponent<span class="token punctuation">:</span><span class="token string">@&quot;main.m&quot;</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token comment">// 当设置为并行队列时在读取文件时实际还是串行</span>
dispatch_queue_t queue <span class="token operator">=</span> <span class="token function">dispatch_queue_create</span><span class="token punctuation">(</span><span class="token string">&quot;queue&quot;</span><span class="token punctuation">,</span> <span class="token constant">NULL</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  
dispatch_fd_t fd <span class="token operator">=</span> <span class="token function">open</span><span class="token punctuation">(</span>path<span class="token punctuation">.</span>UTF8String<span class="token punctuation">,</span> O_RDONLY<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
dispatch_io_t io <span class="token operator">=</span> <span class="token function">dispatch_io_create</span><span class="token punctuation">(</span>DISPATCH_IO_STREAM<span class="token punctuation">,</span> fd<span class="token punctuation">,</span> queue<span class="token punctuation">,</span> <span class="token operator">^</span><span class="token punctuation">(</span><span class="token keyword">int</span> error<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token function">close</span><span class="token punctuation">(</span>fd<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
size_t water <span class="token operator">=</span> <span class="token number">1024</span><span class="token operator">*</span><span class="token number">1024</span><span class="token punctuation">;</span>
<span class="token function">dispatch_io_set_low_water</span><span class="token punctuation">(</span>io<span class="token punctuation">,</span> water<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">dispatch_io_set_high_water</span><span class="token punctuation">(</span>io<span class="token punctuation">,</span> water<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">long</span> <span class="token keyword">long</span> fileSize <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">[</span>NSFileManager defaultManager<span class="token punctuation">]</span> attributesOfItemAtPath<span class="token punctuation">:</span>path error<span class="token punctuation">:</span>nil<span class="token punctuation">]</span><span class="token punctuation">.</span>fileSize<span class="token punctuation">;</span>
NSMutableData <span class="token operator">*</span>totalData <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">[</span>NSMutableData alloc<span class="token punctuation">]</span> init<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token function">dispatch_io_read</span><span class="token punctuation">(</span>io<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> fileSize<span class="token punctuation">,</span> queue<span class="token punctuation">,</span> <span class="token operator">^</span><span class="token punctuation">(</span>bool done<span class="token punctuation">,</span> dispatch_data_t  _Nullable data<span class="token punctuation">,</span> <span class="token keyword">int</span> error<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>error <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        size_t len <span class="token operator">=</span> <span class="token function">dispatch_data_get_size</span><span class="token punctuation">(</span>data<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>len <span class="token operator">&gt;</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token punctuation">[</span>totalData appendData<span class="token punctuation">:</span><span class="token punctuation">(</span>NSData <span class="token operator">*</span><span class="token punctuation">)</span>data<span class="token punctuation">]</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>        
    <span class="token keyword">if</span> <span class="token punctuation">(</span>done<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        NSString <span class="token operator">*</span>str <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">[</span>NSString alloc<span class="token punctuation">]</span> initWithData<span class="token punctuation">:</span>totalData encoding<span class="token punctuation">:</span>NSUTF8StringEncoding<span class="token punctuation">]</span><span class="token punctuation">;</span>
        <span class="token function">NSLog</span><span class="token punctuation">(</span><span class="token string">@&quot;%@&quot;</span><span class="token punctuation">,</span> str<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br></div></div><h4 id="异步并行读取文件"><a href="#异步并行读取文件" class="header-anchor">#</a> 异步并行读取文件</h4> <div class="language-objectivec line-numbers-mode"><pre class="language-objectivec"><code>NSString <span class="token operator">*</span>desktop <span class="token operator">=</span> <span class="token string">@&quot;/Users/XXX/Desktop&quot;</span><span class="token punctuation">;</span>
NSString <span class="token operator">*</span>path <span class="token operator">=</span> <span class="token punctuation">[</span>desktop stringByAppendingPathComponent<span class="token punctuation">:</span><span class="token string">@&quot;main.m&quot;</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
dispatch_queue_t queue <span class="token operator">=</span> <span class="token function">dispatch_queue_create</span><span class="token punctuation">(</span><span class="token string">&quot;queue&quot;</span><span class="token punctuation">,</span> DISPATCH_QUEUE_CONCURRENT<span class="token punctuation">)</span><span class="token punctuation">;</span>
dispatch_fd_t fd <span class="token operator">=</span> <span class="token function">open</span><span class="token punctuation">(</span>path<span class="token punctuation">.</span>UTF8String<span class="token punctuation">,</span> O_RDONLY<span class="token punctuation">)</span><span class="token punctuation">;</span>
dispatch_io_t io <span class="token operator">=</span> <span class="token function">dispatch_io_create</span><span class="token punctuation">(</span>DISPATCH_IO_RANDOM<span class="token punctuation">,</span> fd<span class="token punctuation">,</span> queue<span class="token punctuation">,</span> <span class="token operator">^</span><span class="token punctuation">(</span><span class="token keyword">int</span> error<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token function">close</span><span class="token punctuation">(</span>fd<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
off_t currentSize <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token keyword">long</span> <span class="token keyword">long</span> fileSize <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">[</span>NSFileManager defaultManager<span class="token punctuation">]</span> attributesOfItemAtPath<span class="token punctuation">:</span>path error<span class="token punctuation">:</span>nil<span class="token punctuation">]</span><span class="token punctuation">.</span>fileSize<span class="token punctuation">;</span>
size_t offset <span class="token operator">=</span> <span class="token number">1024</span><span class="token operator">*</span><span class="token number">1024</span><span class="token punctuation">;</span>
dispatch_group_t group <span class="token operator">=</span> <span class="token function">dispatch_group_create</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
NSMutableData <span class="token operator">*</span>totalData <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">[</span>NSMutableData alloc<span class="token punctuation">]</span> initWithLength<span class="token punctuation">:</span>fileSize<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token punctuation">;</span> currentSize <span class="token operator">&lt;=</span> fileSize<span class="token punctuation">;</span> currentSize <span class="token operator">+</span><span class="token operator">=</span> offset<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token function">dispatch_group_enter</span><span class="token punctuation">(</span>group<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">dispatch_io_read</span><span class="token punctuation">(</span>io<span class="token punctuation">,</span> currentSize<span class="token punctuation">,</span> offset<span class="token punctuation">,</span> queue<span class="token punctuation">,</span> <span class="token operator">^</span><span class="token punctuation">(</span>bool done<span class="token punctuation">,</span> dispatch_data_t  _Nullable data<span class="token punctuation">,</span> <span class="token keyword">int</span> error<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>error <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            size_t len <span class="token operator">=</span> <span class="token function">dispatch_data_get_size</span><span class="token punctuation">(</span>data<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>len <span class="token operator">&gt;</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token keyword">const</span> <span class="token keyword">void</span> <span class="token operator">*</span>bytes <span class="token operator">=</span> <span class="token constant">NULL</span><span class="token punctuation">;</span>
                <span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span><span class="token function">dispatch_data_create_map</span><span class="token punctuation">(</span>data<span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token keyword">const</span> <span class="token keyword">void</span> <span class="token operator">*</span><span class="token operator">*</span><span class="token punctuation">)</span><span class="token operator">&amp;</span>bytes<span class="token punctuation">,</span> <span class="token operator">&amp;</span>len<span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token punctuation">[</span>totalData replaceBytesInRange<span class="token punctuation">:</span><span class="token function">NSMakeRange</span><span class="token punctuation">(</span>currentSize<span class="token punctuation">,</span> len<span class="token punctuation">)</span> withBytes<span class="token punctuation">:</span>bytes length<span class="token punctuation">:</span>len<span class="token punctuation">]</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>done<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token function">dispatch_group_leave</span><span class="token punctuation">(</span>group<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token function">dispatch_group_notify</span><span class="token punctuation">(</span>group<span class="token punctuation">,</span> queue<span class="token punctuation">,</span> <span class="token operator">^</span><span class="token punctuation">{</span>
    NSString <span class="token operator">*</span>str <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">[</span>NSString alloc<span class="token punctuation">]</span> initWithData<span class="token punctuation">:</span>totalData encoding<span class="token punctuation">:</span>NSUTF8StringEncoding<span class="token punctuation">]</span><span class="token punctuation">;</span>
    <span class="token function">NSLog</span><span class="token punctuation">(</span><span class="token string">@&quot;%@&quot;</span><span class="token punctuation">,</span> str<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br></div></div><div class="custom-block tip"><p><code>dispatch_io_create_with_path</code>  通过路径创建一个 <code>dispatch_io_t</code> 。与之相似的也有好几个方法，比如 <code>dispatch_io_create_with_io</code> <br> <code>dispatch_io_set_high_water</code>  和 <code>dispatch_io_set_low_water</code>  分割文件大小，分别可以设置一次最少读取和一次最多读取多大。<br> <code>dispatch_io_read</code>  读取文件。与之对应的是 <code>dispatch_io_write</code>  将文件存储到指定路径。如果想提高文件读取速度，可以尝试使用 Dispatch I/O。</p></div></div> <footer class="page-edit" style="display:none;"><!----> <!----></footer> <!----> <!----></main> <!----> <div class="comments-wrapper" data-v-0cb1c278><div class="valine-wrapper"><div id="valine"></div></div></div></div></div></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-a81d141e data-v-a81d141e><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-a81d141e><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-a81d141e></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-a81d141e></path></svg></div><canvas id="vuepress-canvas-ribbon"></canvas><div class="my-popup" style="display:none;" data-v-3414bdeb><div class="my-popup-container" data-v-3414bdeb><div class="my-popup-exit" data-v-3414bdeb></div> <img src="" alt data-v-3414bdeb></div></div><div data-v-2ebabc0c></div><div class="kanbanniang" data-v-3b0f9b07><div class="banniang-container" style="display:;" data-v-3b0f9b07><div class="messageBox" style="right:68px;bottom:230px;display:none;" data-v-3b0f9b07>
      欢迎来到 KnightSama‘s Blog
    </div> <div class="operation" style="right:90px;bottom:40px;display:;" data-v-3b0f9b07><i class="kbnfont kbn-ban-home ban-home" data-v-3b0f9b07></i> <i class="kbnfont kbn-ban-message message" data-v-3b0f9b07></i> <i class="kbnfont kbn-ban-close close" data-v-3b0f9b07></i> <a target="_blank" href="https://vuepress-theme-reco.recoluan.com/views/plugins/kanbanniang.html" data-v-3b0f9b07><i class="kbnfont kbn-ban-info info" data-v-3b0f9b07></i></a> <i class="kbnfont kbn-ban-theme skin" style="display:;" data-v-3b0f9b07></i></div> <canvas id="banniang" width="200" height="260" class="live2d" style="right:90px;bottom:-20px;opacity:0.9;" data-v-3b0f9b07></canvas></div> <div class="showBanNiang" style="display:none;" data-v-3b0f9b07>
    看板娘
  </div></div></div></div>
    <script src="/assets/js/app.a2483de7.js" defer></script><script src="/assets/js/4.0078dadf.js" defer></script><script src="/assets/js/1.c3e7d2b0.js" defer></script><script src="/assets/js/15.92adafd4.js" defer></script>
  </body>
</html>
